package com.scau.data.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.scau.data.entity.ExchangeGoodsDO;
import com.scau.database.entity.storemanager.ExchangeDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ExchangeGoodsMapper extends BaseMapper<ExchangeGoodsDO> {
    /**
     * 获取换货的必要售后信息
     * @param warehosueId
     * @return
     */
    @Select("SELECT \n" +
            "bar_code,\n" +
            "exchange_goods.`count` `count`,\n" +
            "exchange_goods.id deal_id,\n" +
            "exchange_goods.service_id service_id,\n" +
            "custormer_service.`name` service_name,\n" +
            "can_resell can_resell,\n" +
            "`order`.factory_id factory_id,\n" +
            "factory.`name` factory_name,\n" +
            "exchange_goods.factory_exchange_id factory_exchange_id, \n" +
            "exchange_goods.type type, \n" +
            "finished ,\n" +
            "shop_id \n" +
            "FROM\n" +
            "(SELECT id shelf_id,bar_code,warehouse_id\n" +
            "FROM `shelf_ok`\n" +
            "WHERE warehouse_id = #{warehouseId}\n" +
            "UNION \n" +
            "SELECT id,bar_code,warehouse_id \n" +
            "FROM shelf_over\n" +
            "WHERE warehouse_id = #{warehouseId}) AS t1\n" +
//            "JOIN order_line USING(bar_code)\n" +
            "join " +
            "(SELECT * FROM order_line\n" +
            "GROUP BY(bar_code)\n" +
            ") as t2 \n" +
            " USING(bar_code) " +

            "JOIN `order` USING(order_number)\n" +
            "JOIN factory ON `order`.factory_id = factory.id\n" +
            "JOIN shop_line USING(bar_code,shelf_id)\n" +
            "JOIN exchange_goods USING(shop_id,bar_code,shelf_id)\n" +
            "LEFT JOIN custormer_service ON(exchange_goods.service_id = custormer_service.id)\n" +
            "WHERE finished = 0 or finished = 2") // 魔法值0与2分别是未处理与申请枚举值
    List<ExchangeDTO> getExchangeService(@Param("warehouseId") Integer warehosueId);
}
